﻿Imports Amazon.S3
Imports Amazon.S3.Model

#Region "Constants"
Public Enum ESync
    'List of keys is sufficient for inserts (+deletes)
    OneWay_Inserts
    TwoWay_Inserts

    'Dict of keys/LastModified is required for updates
    OneWay_InsertsUpdates
    TwoWay_InsertsUpdates

    'List of keys for inserts/deletes, Dict of keys/LastModified is required for updates
    OneWay_InsertsUpdatesDeletes
End Enum

Public Delegate Sub DNotify(msg As String)
#End Region

Public Class CSync

#Region "Sync - High-level"
    Public Shared Sub SyncFiles(source As CSourceAbstract, destination As CSourceAbstract, sync As ESync)
        Select Case sync
            Case ESync.OneWay_Inserts : OneWay_Inserts(source, destination)
            Case ESync.TwoWay_Inserts : TwoWay_Inserts(source, destination)
            Case Else : Throw New Exception
        End Select
    End Sub


    Public Shared Sub OneWay_Inserts(source As CSourceAbstract, destination As CSourceAbstract)
        Dim local As CKeysAndSizes = source.GetKeysAndSizes
        Dim remote As CKeysAndSizes = destination.GetKeysAndSizes
        Dim adds As CKeysAndSizes = Inserts(local, remote)
        source.CopyTo(destination, adds)
    End Sub
    Public Shared Sub TwoWay_Inserts(source As CSourceAbstract, destination As CSourceAbstract)
        Dim local As CKeysAndSizes = source.GetKeysAndSizes
        Dim remote As CKeysAndSizes = destination.GetKeysAndSizes
        source.CopyTo(destination, Inserts(local, remote))
        destination.CopyTo(source, Inserts(remote, local))
    End Sub
#End Region

#Region "Syncing - Adds/Deletes (Dict of Sizes)"
    Public Shared Function Inserts(source As CKeysAndSizes, destination As CKeysAndSizes) As CKeysAndSizes
        Dim list As New CKeysAndSizes(source.Count)
        For Each i As String In source.Keys
            If Not destination.ContainsKey(i) Then list.Add(i, source(i))
        Next
        Return list
    End Function
    Public Shared Function UpdatesSizeDifferent(source As Dictionary(Of String, Long), destination As Dictionary(Of String, Long)) As List(Of String)
        Dim list As New List(Of String)(source.Count)
        For Each i As String In source.Keys
            If destination.ContainsKey(i) AndAlso destination(i) <> source(i) Then list.Add(i)
        Next
        Return list
    End Function
    Public Shared Function UpdatesPotentialDateDifference(source As Dictionary(Of String, Long), destination As Dictionary(Of String, Long)) As List(Of String)
        Dim list As New List(Of String)(source.Count)
        For Each i As String In source.Keys
            If destination.ContainsKey(i) AndAlso destination(i) = source(i) Then list.Add(i)
        Next
        Return list
    End Function
#End Region


#Region "Syncing - Adds/Deletes (List)"
    Public Shared Function UpdatesPotential(source As List(Of String), destination As List(Of String)) As List(Of String)
        Dim list As New List(Of String)(source.Count)
        For Each i As String In source
            If destination.Contains(i) Then list.Add(i)
        Next
        Return list
    End Function
    Public Shared Function Deletes(source As List(Of String), destination As List(Of String)) As List(Of String)
        Return Inserts(destination, source)
    End Function
    Public Shared Function Inserts(source As List(Of String), destination As List(Of String)) As List(Of String)
        Dim list As New List(Of String)(source.Count)
        For Each i As String In source
            If Not destination.Contains(i) Then list.Add(i)
        Next
        Return list
    End Function
#End Region





End Class
